var env = process.env.NODE_ENV || 'dev';
console.log('应用环境:' + env);

var db_config = require('../db_config.js');
var config = require('../../../webconfig');
var mysql = require('mysql');
var uuid = require('node-uuid');
var dateformat = require('date-format');
var pool = mysql.createPool(eval('db_config.' + env));
var isTrans = false;
var conn = null;

var result = {};


var logSql = function (sql, param, result) {
	if (env == 'dev' && config.debug) {
		console.log(' -- SQL: ', sql);

		if (typeof param != 'undefined') {
			console.log(' -- Param: ', param);
		}

		if (typeof result != 'undefined') {
			console.log(' -- Result: ', result);
		}
	}
}

var execWithNoParams = function (sql, callback) {
	pool.getConnection(function (error, connection) {
		if (error) {
			console.trace('DB-获取数据库连接异常！', error);
			callback(error);
			return;
		}

		conn = connection;

		if (isTrans) {
			connection.beginTransaction(function (err) {
				if (err) {
					console.trace('DB-开启数据库事务异常！', error);
					callback(err);
					return;
				}

				var query = connection.query(sql, function (error, results) {
					if (error) {
						console.trace('DB-执行查询语句异常！', error);
						callback(error);
						return;
					}

					logSql(sql, undefined, results);
					callback(null, results);
				});
			});
		} else {
			var query = connection.query(sql, function (error, results) {
				if (error) {
					console.trace('DB-执行查询语句异常！', error);
					callback(error);
					return;
				}

				connection.release(function (error) {
					if (error) {
						console.trace('DB-关闭数据库连接异常！', error);
						callback(error)
					}
				});

				logSql(query.sql, undefined, results);
				callback(null, results);
			});
		}
	});
};

var execWithParams = function (sql, params, callback) {
	pool.getConnection(function (error, connection) {
		if (error) {
			console.trace('DB-获取数据库连接异常！', error);
			callback(error);
			return;
		}

		conn = connection;

		if (isTrans) {
			connection.beginTransaction(function (err) {
				if (err) {
					console.trace('DB-开启数据库事务异常！', error);
					callback(err);
					return;
				}

				var query = connection.query(sql, params, function (error, results) {
					if (error) {
						console.trace('DB-执行查询语句异常！', error);
						callback(error);
						return;
					}

					logSql(query.sql, params, results);
					callback(null, results);
				});
			})
		} else {
			var query = connection.query(sql, params, function (error, results) {
				if (error) {
					console.trace('DB-执行查询语句异常！', error);
					callback(error);
					return;
				}

				connection.release(function (error) {
					if (error) {
						console.trace('DB-关闭数据库连接异常！', error);
						callback(error)
					}
				});

				logSql(query.sql, params, results);
				callback(null, results);
			});
		}
	});
};

result.exec = function (sql, params, callback) {
	if (arguments.length === 2) {
		execWithNoParams(arguments[0], arguments[1]);
	} else if (arguments.length === 3) {
		execWithParams(sql, params, callback)
	} else {
		console.trace('传入的参数个数不正确。', error)
	}
}

result.beginTrans = function () {
	isTrans = true;
}

result.commit = function (callback) {
	conn.commit(function (error) {
		if (error) {
			console.trace('DB-数据库事务提交异常！', error);
			callback(error);
			return;
		}

		conn.release(function (error) {
			if (error) {
				console.trace('DB-关闭数据库连接异常！', error);
				callback(error)
				return;
			}
			callback(null);
		});
	})
}

result.rollback = function (callback) {
	conn.rollback(function () {
		conn.release(function (error) {
			if (error) {
				console.trace('DB-关闭数据库连接异常！', error);
				callback(error)
				return;
			}
			callback(error)
		});
	})
}

module.exports = result;
exports.release = function (connection, callback) {
	connection.end(function (error) {
		if (error) {
			console.trace('DB-关闭数据库连接异常！', error);
			callback(error)
		}
	});
};

